{% from "macros/table.html" import table, table_decoration %}
{% from "macros/buttons.html" import import_button %}

{% set columns = columns | default([
  {'title': 'name', 'field': 'name', 'sortable': True, 'renderer': None},
  {'title': 'description', 'field': 'description', 'sortable': False, 'renderer': None}
]) %}

{% set data = model_plural_name | get_var %}
{% set table_container = model_name + '-table-container' %}
{% set actions = actions | default([]) %}

<div id="{{ table_container }}" class="table-container" x-data="{ selectedItems: [], exportUrl: '{{ url_for("admin.export_osint_sources") }}' }">
  {% call table_decoration(name, model_name, table_container, routes=routes) %}
    <div class="grow-0">
      <div class="join gap-1" hx-target="#form-container">
        <button hx-post="{{ url_for('admin.collect_osint_source_all') }}" class="btn btn-outline">
          {{ heroicon_outline("arrows-pointing-in", class="h-5 w-5") }}
          Collect All
        </button>
        {{ import_button(url_for('admin.import_osint_sources') ) }}
        <a class="btn btn-outline btn-info"
           :href="exportUrl + (selectedItems.length > 0 ? '?' + selectedItems.map(id => 'ids=' + id).join('&') : '')"
           download>
          {{ heroicon_outline("document-arrow-down", class="h-5 w-5") }}
          Export
        </a>
      </div>
    </div>
  {% endcall %}
  {% if data and data | length > 0 %}
    {{ table(model_name + '-table', data, columns, actions=actions, standalone=false, routes=routes) }}
  {% elif request.args %}
    <div class="text-center p-4">
      <p class="text-gray-500">No {{ name }} available.</p>
      <button class="mt-2 btn btn-primary" hx-get="{{ routes.base_route }}" hx-target="#{{ table_container }}" hx-push-url="true">
        Reset Filter
      </button>
    </div>
  {% else %}
    <div class="text-center p-4">
      <p class="text-gray-500">No {{ name }} available.</p>
      <button class="mt-2 btn btn-primary" hx-post="{{ url_for('admin.load_default_osint_sources') }}" hx-target="#{{ table_container }}">
        Load default {{ name }}
      </button>
    </div>
  {% endif %}
</div>

<script>

document.body.addEventListener('htmx:confirm', async (e) => {
  const elt = e.detail.elt;
  if (!elt.matches('[data-swal-confirm]')) {
    return;
  }

  console.debug('Confirmation needed');

  e.preventDefault();

  const { isConfirmed, value: accept } = await Swal.fire({
    text: "Are you sure you want to delete this OSINT source?",
    input: "checkbox",
    inputValue: 0,
    inputPlaceholder: "Force Deletion of OSINT source and all its data. This action cannot be undone.",
    icon: "question",
    showCancelButton: true,
    confirmButtonText: "Delete"
  });

  if (!isConfirmed) return;

  if (accept) {
    const addForceParam = (evt) => {
      if (evt.detail.elt === elt) {
        evt.detail.parameters.force = true;
      }
    };
    document.body.addEventListener('htmx:configRequest', addForceParam, { once: true });
  }

  e.detail.issueRequest(true);
});

</script>
